package org.upfrost;

import java.sql.DriverManager;
import java.util.Date;

import javax.sql.DataSource;

import junit.framework.Assert;
import junit.framework.TestCase;


import org.junit.After;
import org.junit.Before;
import org.upfrost.Mapper;
import org.upfrost.MapperSession;
import org.upfrost.mapping.classes.TestClass2;
import org.upfrost.mapping.classes.TestClass2DAO;
import org.upfrost.mapping.classes.TestClass3;
import org.upfrost.mapping.classes.TestClass3DAO;
import org.upfrost.testutil.JdbcCall;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class BaseTestClass extends TestCase
{
	protected ComboPooledDataSource m_dataSource;
	protected Mapper m_mapper;
	protected MapperSession m_session;
	protected TestClass2DAO m_dao2;
	protected TestClass3DAO m_dao3;

	@Before
	public void setUp() throws Exception 
	{
		m_dataSource = new ComboPooledDataSource();
		m_dataSource.setDriverClass("org.hsqldb.jdbcDriver");
		m_dataSource.setJdbcUrl("jdbc:hsqldb:mem:test");
		m_dataSource.setUser("sa");
		m_dataSource.setPassword("");
		m_mapper = createMapper(m_dataSource); 
		m_mapper.addAnnotated(TestClass2.class, TestClass3.class);
		createNewSession();
		
		JdbcCall sql = new JdbcCall(m_dataSource);
		sql.open(TestClass2DAO.CREATE_HSQL).update();
		sql.open(TestClass3DAO.CREATE_HSQL).update();
		sql.close();
	}

	@After
	public void tearDown() throws Exception 
	{
		m_session.close();
		JdbcCall sql = new JdbcCall(m_dataSource);
		sql.open(TestClass2DAO.DROP).update();
		sql.open(TestClass3DAO.DROP).update();
		sql.close();
		m_dataSource.close();
		DriverManager.getConnection("jdbc:hsqldb:mem:test;shutdown=true", "sa", "");
	}

	protected void createNewSession()
	{
		if (m_session != null)
			m_session.close();

		m_session = m_mapper.initSession("TestSession").getSession();
		m_dao2 = new TestClass2DAO(m_session);
		m_dao3 = new TestClass3DAO(m_session);
		m_dao2.setCacheExpirationDelayMillis(500);
	}

	protected Mapper createMapper(DataSource datasource)
	{
		return new Mapper(datasource);
	}

	protected void injectTestClass2()
	{
		int count = m_session.createQuery("select count(*) from test2").asClass(Integer.class).single();
		for (int i=0; i<100; i++)
		{
			TestClass2 tc21 = new TestClass2();
			tc21.setId(0);
			tc21.setStrValue("test2:" + (i+1));
			tc21.setSomeDate(new Date());
			m_dao2.insert(tc21);
		}
		int countafter = m_session.createQuery("select count(*) from test2").asClass(Integer.class).single();
		
		Assert.assertEquals(count+100, countafter);
	}

	protected void injectTestClass3()
	{
		int count = m_session.createQuery("select count(*) from test3").asClass(Integer.class).single();
		
		for (int i=0; i<100; i++)
		{
			TestClass3 tc3 = new TestClass3();
			tc3.setId(i+1);
			tc3.setStrValue("test3:" + (i+1));
			tc3.setSomeDate(new Date());
			m_dao3.insert(tc3);
		}
		
		int countafter = m_session.createQuery("select count(*) from test3").asClass(Integer.class).single();
		
		Assert.assertEquals(count+100, countafter);
	}

}
